<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- public quote -->
<%@include file="/jsp/comm_css_include.jsp"%>
<!-- zTreeStyle.css -->
<link rel="stylesheet" type="text/css" href="${basePath}/css/zTreeStyle/zTreeStyle/zTreeStyle.css">
<!doctype html>
<html>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight creditbody">
		 <div class="iboxtitle">
			<span><i class="glyphicon glyphicon-map-marker"></i>&nbsp;系统管理&gt;权限管理&gt;菜单管理</span>
			<a href="javascript:void(0)"></a>
		</div>
         <div class="ibox-content">
         	   <button type="button" class="btn dpbtn_green" onclick="add();">新增</button> 
               <button type="button" class="btn dpbtn_blue" onclick="update();">修改</button> 
               <button type="button" class="btn dpbtn_red" onclick="disable();">禁用</button> 
        	   <button type="button" class="btn dpbtn_red" onclick="enable();">启用</button> 
          </div>
          <div class="ibox-content power" style="overflow:auto;overflow-x: hidden;">	
	          <div id="treeDemo" class="ztree">&nbsp;</div>
          </div>
	</div>
	
	<!-- public quote -->
    <%@include file="/jsp/comm_js_include.jsp"%>

    <!--zTreeStyle.js-->
	<script type="text/javascript" src="${basePath}/js/zTreeStyle/jquery.ztree.core.js" charset="utf-8"></script>
	<script type="text/javascript" src="${basePath}/js/zTreeStyle/jquery.ztree.excheck.js" charset="utf-8"></script>
    
    <!--
    	作者：zhangpp
    	时间：2017-04-06
    	描述：js
    -->
    <script type="text/javascript">
		var winWidth = $(window).width();
		var winHeight = $(window).height();
	    jQuery(function($) {  
	    	getData();
	    	$(".power").css({"height":winHeight / 1.3 + "px"});
	    });
	    
	    
	    var setting = {
	   	      view: {
	   	        selectedMulti: false,
	   	     	nameIsHTML: true  
	   	      },
	   	      data: {
	   	        key: {
	   	          title:""
	   	        },
	   	        simpleData: {
	   	          enable: true
	   	        }
	   	      },
	   	      callback: {
	   	        beforeClick: beforeClick
	   	      }
	    };
	    
	   //菜单父级的编号
	    var parentId = "";
	   
	    //父节点名称
	    var parentName = "";
	    
	    //删除的编号
	    var ids = "";
	    
	    //本身ID
	    var originId = "";
	    
	    
	    //获取节点id和它的子节点id
	    function getId(treeNode){
	    	if(!treeNode) {
	    		return;
	    	}
	    	ids += treeNode.id + ",";//本身ID
	    	if (treeNode.isParent) {
	    		var childrenNodes = treeNode.children;
	    		if (childrenNodes) {
	    			for (var i = 0; i < childrenNodes.length; i++) {
	    				var childrenNode = childrenNodes[i];
	    				getId(childrenNode); //子节点ID
	                }
	    		}
	    	}
	    }
	    var currentNode;
	    function beforeClick(treeId, treeNode) {
	    	
	    	currentNode = treeNode;
	    	/* originId = treeNode.id;
	    	parentName = treeNode.name;
	    	if(parentName && '' != parentName) {
	    		parentName = parentName.split('<')[0];
	    	}
	    	
	    	parentId = treeNode.pId; */
	    	ids = "";
	    	getId(treeNode);
	    }
	    
	    function initIdAndParentId(){
	    	 if(operate == 2) {
	    		if(!currentNode) {
	    			layer.msg("请选择权限修改", {icon: 5});
					return false;
	    		}
	    		var parentNode = currentNode.getParentNode();
	    		if(!parentNode) {
	    			layer.msg("顶级父节点不可操作！", {icon: 5});
					return false;
	    		}
	    		originId = currentNode.id;
		    	parentId = parentNode.id;
		    	parentName = parentNode.name;
		    	if(parentName && '' != parentName) {
		    		parentName = parentName.split('<')[0];
		    	}
	    	} else {
	    		if(!currentNode) {
	    			layer.msg("请选择父权限新增", {icon: 5});
					return false;
	    		}
		    	originId = currentNode.id;
		    	parentId = currentNode.pId;
		    	parentName = currentNode.name;
		    	if(parentName && '' != parentName) {
		    		parentName = parentName.split('<')[0];
		    	}
	    	}
	    	return true;
	    }
	    
	   var operate;
	   var powerList;
	    var getData = function() {
	    	$.dpAjax({
 	    		url:'${basePath}/power/getPowerList',
     			data:{},
     			success: function(datas){
	     			  if(datas.flag == "100100")
					  {
	     				 var  arr = [];
	     				 var  arrDemo = [];
	     				 //先清空数据
	     				 $.fn.zTree.init($("#treeDemo"), setting, arr);
	     				  var d = datas.result;
	     				  for(var i=0;i<d.length;i++)
	     				  {
	     					  var r = d[i];
	     					  var objs = {};
	     					 var objsDemo = {};
	     					  objs.id = r.replaceId;
	     					  objs.pId = r.parentId;
	     					  if(r.dpStatus == 1)
	    					  {
	    						 objs.name = r.name; 
	    					  }
	    					  else if(r.dpStatus == 2)
	    					  {
	    						  objs.name = r.name + '<span style="color:#f05050;">(已被禁用)</span>';
	    					  }
	     					  if(r.powerCode == 'diepu:base'){
		            		  	 objs.open = true;
		            		  	 objsDemo.open = true;
	     					  } else {
	     						 objs.open = false; 
	     						 objsDemo.open = false; 
	     					  }
	     					  arr.push(objs);
	     					  			 
	     					  if(r.dpStatus ==1){
				     			  objsDemo.id = r.replaceId;
				     			  objsDemo.pId = r.parentId;
				     			  objsDemo.name = r.name; 
				     			  arrDemo.push(objsDemo);
	     					  }
	     				  }
	     				  powerList = arrDemo;
	    				  $.fn.zTree.init($("#treeDemo"), setting, arr);
						  return;
					  }
					  if(datas.flag == "100200")
	                  {
						  $.fn.showDatas({"datas":datas});
	                	  return;
	                  }
					  layer.msg(datas.message, {icon: 2});
                 }
 	    	});
	    }
	    
	    var addlayer;
	    var add=function()
	    {	
	    	operate = 1;
			if(initIdAndParentId()){
				if(!originId || originId == '') {
					layer.msg("请先选择节点再进行增加！", {icon: 5});
					return;
				}
				
				addlayer=layer.open({
			    	  type: 2,
			    	  title: '新增菜单',
			    	  shadeClose: true,
			    	  shade: 0.8,
			    	  area: ['500px', '65%'],
			    	  content: 'powerAdd.jsp?parentId='+originId+'&parentName='+parentName,
			    	  end: function () {
			    		  refresh();
			          }
		    	});
			}
	    	
	    }
	    
	    
	    var updatelayer;
	    var update = function() {
	    	operate = 2;
	    	if(initIdAndParentId()){
		    	if(!originId || originId == ''){
					layer.msg("请先选择节点再进行修改！", {icon: 5});
					return;
				}
		    	
		    	if(!parentId || parentId =="") {
		    	   layer.msg("顶级父节点不可操作！", {icon: 5});
		    	   return;
			    }
		    	updatelayer=layer.open({
			    	  type: 2,
			    	  title: '修改菜单',
			    	  shadeClose: true,
			    	  shade: 0.8,
			    	  area: ['500px', '65%'],
			    	  content: 'powerUpdate.jsp?id='+originId+"&parentId=" + parentId +'&parentName='+parentName,
			    	  end: function () {
			    		  refresh();
			          }
		    	});
	    	}
	    	
	    }
	    

	  //禁用
	    var disable = function() {
	    	initIdAndParentId();
	    	if(!ids || ids == '') {
	    		layer.msg("请选择要禁用的权限！", {icon: 5});
	    		return;
	    	}
	        
	    	if(!parentId || parentId =="") {
	    	   layer.msg("顶级父节点不可操作！", {icon: 5});
	    	   return;
			}

			layer.confirm('您确认要禁用吗？', {
	  			  btn: ['确定','取消'] //按钮
	  			},function(){
	            $.dpAjax({
	 	    		url:'${basePath}/power/disable',
	 	    		data: {
	     				"id":ids
	     			},
	     			success: function(datas) {
		     			  if(datas.flag == "100100") {
		     				 layer.msg("执行成功！", {icon: 6});
		     				 refresh();
							 return;
						  }
						  if(datas.flag == "100200") {
							  $.fn.showDatas({"datas":datas});
		                	  return;
		                  }
						  layer.msg(datas.message, {icon: 2});
	                 }
	 	    	});
	  		});	
	    }
	    
	    
	  //启用
	    var enable = function() {
	    	initIdAndParentId();
	    	if(!ids || ids == '') {
	    		layer.msg("请选择要启用的权限！", {icon: 5});
	    		return;
	    	}
	        
	    	if(!parentId || parentId =="") {
	    	   layer.msg("顶级父节点不可操作！", {icon: 5});
	    	   return;
		    }

			layer.confirm('您确认要启用吗？', {
	  			  btn: ['确定','取消'] //按钮
	  			},function(){
	  			layer.closeAll();
	            $.dpAjax({
	 	    		url:'${basePath}/power/enable',
	 	    		data: {
	     				"id":ids
	     			},
	     			success: function(datas) {
		     			  if(datas.flag == "100100")  {
		     				  layer.msg("执行成功！", {icon: 6});
		     				 refresh();
							  return;
						  }
						  if(datas.flag == "100200")  {
							  $.fn.showDatas({"datas":datas});
		                	  return;
		                  }
						  layer.msg(datas.message, {icon: 2});
	                 }
	 	    	});
	  		});	
	    }
		
	  /* var del = function() {
	            $.dpAjax({
	 	    		url:'${basePath}/power/del',
	 	    		data:
	 	    		{
	     				"id":ids
	     			},
	     			success: function(datas) {
		     			  if(datas.flag == "100100") {
		     				  layer.msg("执行成功！", {icon: 6});
		     				 refresh();
							  return;
						  }
						  if(datas.flag == "100200")  {
							  $.fn.showDatas({"datas":datas});
		                	  return;
		                  }
						  layer.msg(datas.message, {icon: 2});
	                 }
	 	    	});
	  	
	    } */
	   
	    var refresh = function() {	
	    	getData();
	    	// location.reload();
	    }
    </script>
</body>
</html>
